use([
  "public/javascripts/service/billManage_tradeService.js",
  "public/javascripts/widget/slider/slider.js",
  "public/javascripts/widget/mtable/mtable.js",
  "public/javascripts/widget/calander/calander.js",
  "public/javascripts/widget/select/select.js",
  "public/javascripts/widget/dialog/dialog.js",
  "public/javascripts/widget/prompt/prompt.js"
], function(billManage_tradeService, SliderComponent, mtableModule, Calander, selectFactory, DialogComponent, PromptComponent) {
    var PageComponent = React.createClass({
        mtable: null,
        restData:{
            'billNo':'',
            'orderNo':'',
            'status':'',
            'platformId':'',
            'startEffectTime':'',
            'endEffectTime':'',
            'pageIndex':1,
            'pageSize':10
        },
        componentDidMount: function() {
          var _this = this;

          _this.mtable = mtableModule.init('#mtable');

          Calander.init({ele: ['#start-time',"#end-time"],isClear:true});

          $('#start-time').val(new Date().getLastMonth().start);
          $('#end-time').val(new Date().getLastMonth().end);


            //2：贸易客户；128：贸易供应商
          var userType = cookie.get('userType');
          switch(userType){
            case '2': _this.userType = 'cus';break;
            case '3': _this.userType = 'cus';break;
            case '256': _this.userType = 'sup';break;
          }
          //_this.userType = cookie.get('userType') == 2 ? 'cus' : 'sup';
          _this.primary = cookie.get('primary') == 'true' ? true : false;
          _this.padding = _this.primary? '0px 20px 20px 50px' : '0px 20px 20px 20px';
          _this.search();

          _this.setState({
            'userType':_this.userType,
            'primary':_this.primary,
            'padding':_this.padding
          });
        },
        receiveMsg: function(data) {

        },
        getInitialState: function () {
            return this.props ? this.props : {};
        },
        render:function() {
            var data = this.state.data;

            var  _this = this,
                list = this.state.list ? this.state.list : [],
                Browser = typeof window != 'undefined',
                totalResult = _this.state.totalResult;

            return ~init;
        },
        getStatus: function(){
          var _this = this;

          if(_this.statusList){ return; }

          var config = {
            'seatParams':{
              'interType':_this.userType
            }
          };

          billManage_tradeService.getStatus(config, function(res){
            if(res.errorCode === 0){
              _this.statusList = res.data;
              selectFactory({
                data: res,
                id: "status",
                attrTextField: {
                  'data-code':'code'
                },
                attrTextId: function(code){
                  _this.restData.status = code ? code : '';
                }
              }).open();
            }
          });
        },
        getPlatform: function(){
          var _this = this;
          if(_this.platformList){ return; }

          var config = {
            'seatParams':{
              'interType':_this.userType
            }
          };

          billManage_tradeService.getPlatform(config, function(res){
            if(res.errorCode === 0){
              _this.platformList = res.data;
              selectFactory({
                data: res,
                id: "platform",
                attrTextField: {
                  'data-id':'id'
                },
                attrTextId: function(id){
                  _this.restData.platformId = id ? id : '';
                }
              }).open();
            }
          });
        },
        search: function(isReset){
          var _this = this;

          _this.restData.billNo = $.trim($('#bill-num').val());
          _this.restData.orderNo = $.trim($('#order-num').val());
          _this.restData.startEffectTime = $('#start-time').val();
          _this.restData.endEffectTime = $('#end-time').val();
          isReset ? _this.restData.pageIndex = 1 : '';

          var config = {
              'urlParams':{
                  'billNo': _this.restData.billNo,
                  'orderNo': _this.restData.orderNo,
                  'status': _this.restData.status,
                  'platformId': _this.restData.platformId,
                  'startEffectTime': _this.restData.startEffectTime,
                  'endEffectTime': _this.restData.endEffectTime,
                  'pageIndex':_this.restData.pageIndex,
                  'pageSize':_this.restData.pageSize
              },
              'seatParams':{
                'interType':_this.userType
              }
          };

          billManage_tradeService.getTable(config, function(res){
            if(res.errorCode === 0){
              _this.refresh(res);
              _this.mtable.cancelSelect();
            }
          });
        },
        reset: function(){
          var _this = this;
          $("#bill-num,#order-num,#platform,#status").val('');
          _this.restData.status = '';
          _this.restData.platformId = '';

          $('#start-time').val(new Date().getLastMonth().start);
          $('#end-time').val(new Date().getLastMonth().end);
          _this.search(true);
        },
        refresh: function(res){
          var _this = this,
              data = res.data,
              arr = [];

          for(var i=0, l=data.length; i<l; i++){
              arr.push(data[i]);
          }
          _this.setState({list:arr,'totalResult':res.pagination.totalResult});
        },
        onPageChange: function(pageIndex,pageSize){
          var _this = this;

          _this.restData.pageIndex = pageIndex;
          _this.restData.pageSize = pageSize;

          _this.search();
        },
        export: function(){
          var _this = this;
          var selected = _this.mtable.getSelectData();

          if(!selected.length){
              PromptComponent.init({'type':'warning','msg':'请先选择数据！'}).show();
              return;
          }

          DialogComponent.init({
              'type':'delete',
              'msg':'确定要导出所选账单吗？',
              'success':function(){
                  var params = [];
                  for(var i=0, l=selected.length; i<l; i++){
                      params.push(selected[i].no);
                  }

                  var config = {
                      'urlParams':params,
                      'seatParams':{
                        'interType': _this.userType
                      }
                  };

                  billManage_tradeService.export(config, function(res){
                    if(res.errorCode === 0){
                      res.data.forEach(function(item){
                				_this.downloadFile(item.url);
                			});
                    }
                  });
              }
          }).show();
        },
        downloadFile : function(url) {
            try{
                var elemIF = document.createElement("iframe");
                elemIF.src = url;
                elemIF.style.display = "none";
                document.body.appendChild(elemIF);
            }catch(e){}
        }
    });
    if(typeof ReactDOM != "undefined") {
        ReactDOM.render(<PageComponent data={storage}/>, document.getElementById('container'));
    }
    module.exports = PageComponent;
});
